<template>
  <div class="article-container">
    <el-card class="filter-card">
      <div slot="header" class="clearfix">
        <!-- 路径导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 路径导航 -->
      </div>
      <!-- 表单数据 -->
      <el-form ref="form" label-width="80px" size="mini">
        <el-form-item label="状态">
          <el-radio-group v-model="status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道">
          <el-select v-model="channelId" placeholder="请选择频道">
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="item in channels"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="rangeDate"
            format="yyyy-MM-dd"
            type="datetimerange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="loadArticles(1)"
            :disabled="pagenationDisabled"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 表单数据 -->
    </el-card>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        根据筛选条件共查询到{{ totalCount }}个查询结果
      </div>
      <!-- 数据列表 -->
      <el-table
        v-loading="loading"
        :data="articles"
        stripe
        class="list-table"
        size="small"
      >
        <el-table-column prop="cover" label="封面">
          <template slot-scope="scope">
            <el-image
              style="width: 70px; height: 50px"
              :src="scope.row.cover.images[0]"
              fit="cover"
              lazy
            >
              <div slot="placeholder" class="image-slot">
                加载中<span class="dot">...</span>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column prop="status" label="状态">
          <!-- 获取当前遍历项数据 -->
          <template slot-scope="scope">
            <el-tag :type="articlesStatus[scope.row.status].type">{{
              articlesStatus[scope.row.status].text
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="pubdate" label="发布时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              circle
              type="primary"
              icon="el-icon-edit"
                @click="editArticle(scope.row)"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              circle
              icon="el-icon-delete"
              @click="onDeleteArticle(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 数据列表 -->
      <el-pagination
        background
        layout="prev, pager, next"
        :total="totalCount"
        :page-size="pageSize"
        :current-page.sync="page"
        @current-change="onCurrentChange"
        :disabled="pagenationDisabled"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import {
  getArticles,
  getArticleChannels,
  deleteArticle
} from '@/api/article.js'
export default {
  name: 'Article',
  data () {
    return {

      articles: [],
      articlesStatus: [
        {
          text: '草稿',
          status: 0,
          type: 'info'
        },
        {
          text: '待审核',
          status: 1,
          type: ''
        },
        {
          text: '审核通过',
          status: 2,
          type: 'success'
        },
        {
          text: '审核失败',
          status: 3,
          type: 'warning'
        },
        {
          text: '已删除',
          status: 4,
          type: 'danger'
        }
      ],
      channels: [],
      totalCount: 0,
      page: 1,
      pageSize: 10,
      status: null,
      channelId: null,
      rangeDate: [], // 筛选日期
      loading: true,
      pagenationDisabled: false
    }
  },
  props: {},
  components: {},
  mounted () {},
  created () {
    this.loadArticleChannels()
    this.loadArticles(1)
  },
  methods: {
    onSubmit () {},
    loadArticles (page) {
      this.loading = true
      this.pagenationDisabled = true
      getArticles({
        page,
        per_page: this.pageSize,
        status: this.status,
        channel_id: this.channelId,
        begin_pubdate: this.rangeDate ? this.rangeDate[0] : null,
        end_pubdate: this.rangeDate ? this.rangeDate[1] : null
      })
        .then((res) => {
          const { results, total_count: totalCount } = res.data.data
          this.articles = results
          this.totalCount = totalCount
          this.loading = false
          this.pagenationDisabled = false
        })
        .catch((e) => {
          this.loading = false
          this.pagenationDisabled = false
        })
    },
    // 加载文章频道
    loadArticleChannels () {
      getArticleChannels()
        .then((res) => {
          this.channels = res.data.data.channels
        })
        .catch((e) => {})
    },

    onCurrentChange (page) {
      this.loadArticles(page)
    },
    onDeleteArticle (articleId) {
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteArticle(articleId.toString())
            .then((res) => {
              this.$message({
                type: 'success',
                message: '删除成功'
              })
              this.loadArticles(this.page)
            })
            .catch((e) => {})
        })
        .catch(() => {})
    },
    // 编辑文章
    editArticle (article) {
      this.$router.push('/publish?id=' + article.id)
    }
  }
}
</script>

<style lang="less" scoped>
.filter-card {
  margin-bottom: 20px;
}

.list-table {
  width: 100%;
  margin-bottom: 20px;
}

.article-cover {
  width: 100px;
  background-size: cover;
}
</style>
